package com.wsoft.utils;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;


/**
 * @Author Zhang gukai
 * @Date 2024/11/11 13:49
 */
@Component
public class Sm3EncryptionUtil {

    /**
     * sm3加密
     * @param data
     * @return
     */
    public static String encrypt(String data) {
        byte[] bytes = data.getBytes();
        byte[] hash = hash(bytes);
        return ByteUtils.toHexString(hash);
    }

    public static byte[] hash(byte[] srcData)  {
        SM3Digest digest = new SM3Digest();
        digest.update(srcData,0,srcData.length);
        byte[] bytes = new byte[digest.getDigestSize()];
        digest.doFinal(bytes,0);
        return bytes;
    }

    }

